package br.com.musclebook.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import br.com.musclebook.enums.ExerciseCategory;

@Entity
@Table(name = "EXERCISE_EXECUTION_SET")
public class ExerciseExecutionSet implements Serializable, Comparable<ExerciseExecutionSet> {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "ROUTINE_DAY_TRAINING_ID")
    private RoutineDayTraining routineDayTraining;

    @Column(name = "ORDER_NUMBER")
    private Integer orderNumber;

    @ManyToOne
    @JoinColumn(name = "EXERCISE_ID")
    private Exercise exercise;

    @OneToMany(mappedBy = "exerciseExecutionSet", cascade = { CascadeType.ALL }, orphanRemoval = true)
    @OrderBy("orderNumber")
    private Set<ExecutionSet> executionSets;

    public ExerciseExecutionSet() {
	this.executionSets = new TreeSet<ExecutionSet>();
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public RoutineDayTraining getRoutineDayTraining() {
	return routineDayTraining;
    }

    public void setRoutineDayTraining(RoutineDayTraining routineDayTraining) {
	this.routineDayTraining = routineDayTraining;
    }

    public Integer getOrderNumber() {
	return orderNumber;
    }

    public void setOrderNumber(Integer orderNumber) {
	this.orderNumber = orderNumber;
    }

    public Exercise getExercise() {
	return exercise;
    }

    public void setExercise(Exercise exercise) {
	this.exercise = exercise;
    }

    public Set<ExecutionSet> getExecutionSets() {
	return this.executionSets;
    }

    public List<ExecutionSet> getExecutionSetsAsList() {
	return new ArrayList<ExecutionSet>(executionSets);
    }

    public void setExecutionSets(Set<ExecutionSet> executionSets) {
	this.executionSets = executionSets;
    }

    public boolean addExecutionSet(ExecutionSet executionSet) {
	return this.executionSets.add(executionSet);
    }

    public boolean removedExecutionSet(ExecutionSet executionSet) {
	return this.executionSets.remove(executionSet);
    }

    @Override
    public int compareTo(ExerciseExecutionSet o) {
	String thisRoutineName = this.getRoutineDayTraining().getRoutineDay().getRoutine().getName();
	String thatRoutineName = o.getRoutineDayTraining().getRoutineDay().getRoutine().getName();

	Integer thisRoutineDay = this.getRoutineDayTraining().getRoutineDay().getDayOfWeek();
	Integer thatRoutineDay = o.getRoutineDayTraining().getRoutineDay().getDayOfWeek();

	ExerciseCategory thisExercCategory = this.getRoutineDayTraining().getExerciseCategory();
	ExerciseCategory thatExercCategory = o.getRoutineDayTraining().getExerciseCategory();

	if (thisRoutineName.equals(thatRoutineName)) { // compare name
	    if (thisRoutineDay.equals(thatRoutineDay)) { // compare day
		if (thisExercCategory.equals(thatExercCategory)) { // compare category
		    return this.orderNumber.compareTo(o.getOrderNumber());
		} else {
		    return thisExercCategory.compareTo(thatExercCategory);
		}
	    } else {
		return thisRoutineDay.compareTo(thatRoutineDay);
	    }
	} else {
	    return thisRoutineName.compareTo(thatRoutineName);
	}
    }
}
